package two_xia.ex7.Demo02;

import java.util.HashSet;
import java.util.Set;

public class DeptComponent extends OrganizationComponent {
    public Set<OrganizationComponent> organizationCompoent = new HashSet<>();
    private String name;

    public String getName() {
        return this.name;
    }

    public void setName(String newName) {
        this.name = newName;
    }

    public void addComponent(OrganizationComponent organizationComponent) {
        organizationCompoent.add(organizationComponent);
    }

    public void removeComponent(OrganizationComponent organizationComponent) {
        organizationCompoent.remove(organizationComponent);
    }

    public OrganizationComponent getChild() {
        System.out.println("获取子代元素");
        return null;
    }

    public void sendMessage(String msg) {
        System.out.println(this.getName() + "发送公文");
        for (OrganizationComponent oc : organizationCompoent) {
            if (oc instanceof DeptComponent) {
                oc.sendMessage(msg);
            } else if (oc instanceof Office) {
                oc.onMessage();
            }
        }
    }

    public void onMessage() {
        System.out.println("错误方法");
    }
}